package com.rjpacket.supernews.app;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.rjpacket.supernews.app.adapters.ViewPagerCommonAdapter;
import com.rjpacket.supernews.app.fragments.SaveArticleFragment;
import com.rjpacket.supernews.app.fragments.SaveImageFragment;

import java.util.LinkedList;


public class MySaveActivity extends BaseActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    private ViewPager viewPager;
    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_save);

        TextView actionBar = (TextView) findViewById(R.id.actionbar_title);
        actionBar.setText("我的收藏");

        viewPager = (ViewPager) findViewById(R.id.save_viewPager);

        LinkedList<Fragment> fragments = new LinkedList<Fragment>();
        fragments.add(new SaveArticleFragment());
        fragments.add(new SaveImageFragment());

        ViewPagerCommonAdapter adapter = new ViewPagerCommonAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);

        group = (RadioGroup) findViewById(R.id.save_middle);
        group.setOnCheckedChangeListener(this);

        View view = group.getChildAt(0);
        if (view != null && view instanceof RadioButton) {
            RadioButton rb = (RadioButton) view;
            rb.setChecked(true);
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        View childAt = group.getChildAt(i);
        if (childAt != null && childAt instanceof RadioButton) {
            RadioButton rb = (RadioButton) childAt;
            rb.setChecked(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int count = group.getChildCount();
        int index = 0;
        for (int i = 0; i < count; i++) {
            View view = group.getChildAt(i);
            if (view != null && view instanceof RadioButton) {
                RadioButton rb = (RadioButton) view;
                if (rb.isChecked()) {
                    index = i;
                    break;
                }
            }
        }
        viewPager.setCurrentItem(index);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }
}
